package   ru.mipt.board.DB
import ru.mipt.board.Analysis._
import java.sql.{Connection,DriverManager}
import ru.mipt.board.net._
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.adapters.MySQLAdapter
import org.squeryl._

/**
 * Created by IntelliJ IDEA.
 * User: IL
 * Date: 07.10.2010
 * Time: 20:05:47
 * To change this template use File | Settings | File Templates.
 */

trait DBuser{
    Class.forName("com.mysql.jdbc.Driver");
    val connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=qaz")
    connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED)
    import testdb._

    def insertMessages(messages: List[BoardMessage]) = {
        using(Session.create(connection,new MySQLAdapter)){
            MESSAGES.insert(messages)
        }
    }

    def inDB[T](a: => T): T = {
        using(Session.create(connection,new MySQLAdapter)){ a }
    }

    def getAllMessagesID():List[Int] ={
         using(Session.create(connection,new MySQLAdapter)){
            val a = from(MESSAGES)(s => select(s.ID)).toList
            return a
        }
    }
}

object  testdb extends Schema{
    val MESSAGES = table[BoardMessage]("MESSAGES1")
        on(MESSAGES)( m => declare(
            m.ID      is(unique,indexed),
            m.text    is(dbType("TEXT"))
        ))
    val USERS1 = table[Word]("USERS1")
    val words1 = table[Word]("WORDS1")
}

